package org.lendoo.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.lendoo.web.enumeration.GoodsInCartSelectedTypeEnum;
import org.lendoo.web.enumeration.ServiceResultEnum;
import org.lendoo.web.pojo.common.ServiceResult;
import org.lendoo.web.pojo.dto.CartWithGoodsAndImageDto;
import org.springframework.stereotype.Service;
import org.lendoo.web.mapper.CartMapper;
import org.lendoo.web.entity.Cart;
import org.lendoo.web.service.CartService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * CartService接口的实现类
 */
@Slf4j
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Resource
    private ServiceHelper serviceHelper;

    /**
     * 新增
     * @param cart 购物车表
     * @return 添加成功返回1，添加失败返回-1
     */
    @Override
    public ServiceResult<Boolean> add(Cart cart) {
        LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(Cart::getGoodsId, cart.getGoodsId());
        cartLambdaQueryWrapper.eq(Cart::getUserId, cart.getUserId());
        Cart theCart = baseMapper.selectOne(cartLambdaQueryWrapper);
        if (null == theCart){
            Date date = new Date();
            cart.setCreateTime(date);
            cart.setUpdateTime(date);
            baseMapper.insert(cart);
        } else {
            theCart.setQuantity(theCart.getQuantity().intValue() + cart.getQuantity().intValue());
            Date date = new Date();
            theCart.setUpdateTime(date);
            baseMapper.updateById(theCart);
        }
        return new ServiceResult<>(ServiceResultEnum.INSERT_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.INSERT_SUCCESS.getMessage());
    }

    /**
     * 批量导入
     * @param cartList 购物车表（列表）
     * @return 批量导入成功返回1，批量导入失败返回-1
     */
    @Override
    public ServiceResult<Boolean> batchAdd(List<Cart> cartList) {
        baseMapper.insertBatchSomeColumn(cartList);
        return new ServiceResult<>(ServiceResultEnum.INSERT_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.INSERT_SUCCESS.getMessage());
    }

    /**
     * 根据id更新
     * @param cart 购物车表
     * @return 更新成功返回1，更新失败返回-1
     */
    @Override
    public ServiceResult<Boolean> updateForId(Cart cart) {
        baseMapper.updateById(cart);
        return new ServiceResult<>(ServiceResultEnum.UPDATE_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.UPDATE_SUCCESS.getMessage());
    }

    /**
     * 根据id删除
     * @param id 主键
     * @return 删除成功返回1，删除失败返回-1
     */
    @Override
    public ServiceResult<Boolean> deleteById(Serializable id) {
        baseMapper.deleteById(id);
        return new ServiceResult<>(ServiceResultEnum.DELETE_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.DELETE_SUCCESS.getMessage());
    }

    /**
     * 根据id查询
     * @param id 主键
     * @return 返回查询结果
     */
    @Override
    public ServiceResult<Cart> selectById(Serializable id) {
        Cart cart = baseMapper.selectById(id);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), cart, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 根据条件，分页查询，升序/降序排列
     * @param cart 购物车表
     * @param pageNo 页号
     * @param pageSize 每页行数
     * @return 返回查询结果列表
     */
    @Override
    public ServiceResult<PageInfo<List<CartWithGoodsAndImageDto>>> page(Cart cart, Integer pageNo, Integer pageSize) {
//        LambdaQueryWrapper<Cart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        List<Cart> cartList = baseMapper.selectList(lambdaQueryWrapper);
        List<CartWithGoodsAndImageDto> cartWithGoodsAndImageDtoList = serviceHelper.getCartMapper().page(cart.getUserId(), pageNo-1, pageSize);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), new PageInfo(cartWithGoodsAndImageDtoList), Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 查询所有记录
     * @return
     */
    @Override
    public ServiceResult<List<Cart>> findAll() {
        List<Cart> cartList = baseMapper.selectList(null);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), cartList, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 如果购物车中这个用户的这个商品已经存在，则加一；如果不存在则创建这条记录
     * @param cart
     * @return
     */
    @Override
    public ServiceResult<Boolean> saveOrIncrementCart(Cart cart) {
        LambdaQueryWrapper<Cart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartLambdaQueryWrapper.eq(Cart::getUserId, cart.getUserId());
        cartLambdaQueryWrapper.eq(Cart::getGoodsId, cart.getGoodsId());
        Cart theCart = baseMapper.selectOne(cartLambdaQueryWrapper);
        if (null == theCart) {
            cart.setQuantity(1);
            cart.setSelected(GoodsInCartSelectedTypeEnum.NOT_SELECTED.getType().intValue());
            cart.setCreateTime(new Date());
            cart.setUpdateTime(new Date());
            baseMapper.insert(cart);
            return new ServiceResult<>(ServiceResultEnum.INSERT_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.INSERT_SUCCESS.getMessage());
        } else {
            theCart.setQuantity(theCart.getQuantity() + 1);
            theCart.setUpdateTime(new Date());
            baseMapper.updateById(theCart);
            return new ServiceResult<>(ServiceResultEnum.UPDATE_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.UPDATE_SUCCESS.getMessage());
        }
    }
}
